package com.yd.mvc.mobile.oldsRetrieveController;

import com.yd.app.service.common.LoggerService;
import com.yd.app.service.mobile.oldsRetriveService.MOldsRetrieveService;
import com.yd.app.service.pc.gtypeService.GTypeService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.GType;
import com.yd.domain.business.Old;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.OldParam;
import com.yd.domain.sys.Logger;
import com.yd.domain.sys.PermEnum;
import com.yd.domain.sys.User;
import com.yd.mvc.baseController.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-08 10:23
 * @desc
 **/
@RestController
@RequestMapping(value="/mapi/moldsRetrieve")
public class MOldsRetrieveController extends BaseController {
    @Autowired
    private MOldsRetrieveService oldsRetrieveService;
    @Autowired
    private LoggerService loggerService;
    @Autowired
    private GTypeService gTypeService;
    /**
     * @Author: zy
     * @Desc:查询数据旧物回收列表
     * @Date:  2018/3/8
     */
    @RequestMapping(value = "/list/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public PageModel getOldList(@RequestBody OldParam oldParam, @PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize)throws Exception {
        PageModel pm=oldsRetrieveService.getOldList(oldParam,pageIndex,pageSize);
        return pm;
    }
    /**
     * @Author: zy
     * @Desc: 录入旧物
     * @Date:  2018/4/12
     * @Param
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void add(@RequestBody Old old, HttpServletRequest request, HttpSession session)  throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        String user="";
        if(u!=null){
            user=u.getId();
            String suff="ID:"+old.getId()+";名称:"+old.getTitle()+";重量:"+old.getPerWeight();
            try {
                old.setAddUser(user);//录入人
                oldsRetrieveService.addOld(old);
                loggerService.addLogger(new Logger("新增旧物记录",user,Logger.LOG_TYPE.ADD,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
            } catch (Exception e) {
                loggerService.addLogger(new Logger("新增旧物记录",user,Logger.LOG_TYPE.ADD,Logger.STATUS.FAIL,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
                e.printStackTrace();
            }
        }else{
            throw new BusinessException(401,"尚未登录","no-login");
        }
    }
    /**
     * @Author: zy
     * @Desc: 查询商品类型
     * @Date:  2018/4/12
     * @Param
     */
    @RequestMapping(value = "/getGoodType/{goodKind}",method = RequestMethod.GET)
    public List<GType> getGoodType(@PathVariable("goodKind") String goodKind){
        List<GType> list =new ArrayList<>();
        list=gTypeService.getGoodType(goodKind);
        return list;
    }

    /**
     * 根据用户查询"我的旧物"
     * @Author: sxh
     * @param pageIndex
     * @param pageSize
     */
    @RequestMapping(value = "/getMyOldList/{pageIndex}/{pageSize}", method = RequestMethod.POST)
    public List<Old> getMyOldList(@RequestBody OldParam oldParam,HttpSession session,@PathVariable("pageIndex")int pageIndex, @PathVariable("pageSize")int pageSize)throws Exception {
        User u = (User) session.getAttribute("login_user");
        List<Old> list=new ArrayList<>();
        if(u!=null){
            try {
                if(!u.getPermCodeList().contains(PermEnum.OLD_MANAGER)) {//是否具有旧物管理管理权限
                    oldParam.setAddUser(u.getId());
                }
                list=oldsRetrieveService.getMyOldList(oldParam,pageIndex,pageSize);
            } catch (Exception e) {
                 throw new BusinessException(500, "获取订单信息失败");
            }
        }

        return list;
    }
    /**
     * @Author: zy
     * @Desc: 根据id查询
     * @Date:  2018/4/13
     * @Param
     */
    @RequestMapping(value = "/getOldByID/{id}",method = RequestMethod.GET)
    public Old getOldByID(@PathVariable("id") String id){
        Old old =oldsRetrieveService.getOldByID(id);
        if(old==null){
            old=new Old();
        }
        return old;
    }
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateOld(@RequestBody Old old,HttpServletRequest request, HttpSession session) throws BusinessException {
        User u = (User) session.getAttribute("login_user");
        String user="";
        if(u!=null){
            String suff="ID:"+old.getId()+";名称:"+old.getTitle()+";重量:"+old.getPerWeight()+";商品类型:"+old.getType();
            try {
                oldsRetrieveService.updateOld(old);
                loggerService.addLogger(new Logger("修改旧物记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.SUCCESS,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
            } catch (Exception e) {
                loggerService.addLogger(new Logger("修改旧物记录",user,Logger.LOG_TYPE.UPDATE,Logger.STATUS.FAIL,Logger.MODEL_TYPE.OLDRETRIEVE,"",suff),request);
                e.printStackTrace();
            }
        }else{
            throw new BusinessException(401,"尚未登录","no-login");
        }

    }
}
